import { View } from "../../../../mvc/core/View";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ObjCreate } from "../../common/ObjCreate";
import { DeployPanelItem } from "../../equipment/deploy/DeployPanelItem";
import { Language } from "../../../../../extend/language/Language";
import { MountEquipRoundProcess } from "../mount/MountEquipRoundProcess";
import { BaseItem } from "../../common/BaseItem";
import { MountEquipAttSp } from "../mountEquip/MountEquipAttSp";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { TilePanel } from "../../equipment/TilePanel";
import { MountEquipCellRender } from "../cell/MountEquipCellRender";
import { ListEvent } from "../../../../../fl/events/ListEvent";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { Cache } from "../../../cache/Cache";
import { MountEquipCellInfo } from "../mountEquip/MountEquipCellInfo";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { ItemData } from "../../../resource/info/ItemData";
import { MountConfig } from "../../../resource/MountConfig";
import { MsgManager } from "../../../manager/MsgManager";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { EquipmentItemDataUtil } from "../../common/EquipmentItemDataUtil";
import { MountEquipAttInfo } from "../mountEquip/MountEquipAttInfo";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { EJewel } from "../../../../../Message/Public/EJewel";
import { CommonBmdUtil } from "../../common/util/CommonBmdUtil";
import { ItemExInfo } from "../../../resource/info/ItemExInfo";
import { TMountEquipUpgrade } from "../../../../../Message/Db/Tables/TMountEquipUpgrade";
type int = number;
//class MountEquipUpgrade
    
    export  class MountEquipUpgrade extends View
    {
        constructor()
        {
            super();
            this.initView();
            return;
        }

        /* internal  */initView(): void
        {
            var loc2=null;
            this._bg = new GImageBitmap(ImagesConst.mountEquipBg, 0, 0);
            addChild(this._bg);
            ObjCreate.createLoadedBitmap("Label_jipingyulang.png", 7, 6, this);
            this._jpyl = new DeployPanelItem(0);
            this._jpyl.x = 27;
            this._jpyl.y = 38;
            this._jpyl.label = Language.getString(61091);
            addChild(this._jpyl);
            this._equipNameTx = ObjCreate.createGTextField("", 121, 17, 126, 22, this);
            this._equipNameTx.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._roundPro = new MountEquipRoundProcess();
            this._roundPro.x = 106;
            this._roundPro.y = 37;
            addChild(this._roundPro);
            ObjCreate.createLoadedBitmap(ImagesConst.littleStar_big, 332, 24, this);
            this._attBmRight = ObjCreate.createBitmap("", 37, 86, this);
            this._attBmLeft = ObjCreate.createBitmap("", 265, 86, this);
            ObjCreate.createBg(150, 81, 65, 65, this, ImagesConst.PackItemBg);
            this._equipItem = new BaseItem();
            this._equipItem.setSize(60, 60);
            this._equipItem.isShowLock = false;
            this._equipItem.x = 153;
            this._equipItem.y = 84;
            this.addChild(this._equipItem);
            this._leftAtt = new MountEquipAttSp(true);
            this._rightAtt = new MountEquipAttSp(false);
            this._leftAtt.x = 10;
            this._leftAtt.y = 192;
            this._rightAtt.x = 195;
            this._rightAtt.y = 192;
            addChild(this._leftAtt);
            addChild(this._rightAtt);
            var loc1=GlobalClass.getBitmap(ImagesConst.EquipRecast_arrow);
            loc1.x = 163;
            loc1.y = 250;
            this.addChild(loc1);
            ObjCreate.createBg(374, 0, 164, 396, this);
            ObjCreate.createBg(376, 3, 139, 24, this, ImagesConst.TileBg);
            this._equipList = new TilePanel();
            this._equipList.x = 380;
            this._equipList.y = 28;
            this._equipList.setSize(155, 230);
            this._equipList.columnWidth = 178;
            this._equipList.rowHeight = 50;
            this._equipList.setStyle("cellRenderer", MountEquipCellRender);
            this._equipList.addEventListener(ListEvent.ITEM_CLICK, this.onListItemClickHandler);
            this._equipList.selectedIndex = 0;
            this.addChild(this._equipList);
            loc2 = ObjCreate.createTextArea("", 381, 240, 151, 154, this);
            loc2.textField.filters = [FilterConst.glowFilter];
            loc2.htmlText = Language.getString(74219);
            loc2.wordWrap = true;
            loc2.drawNow();
            LoaderHelp.addResCallBack(ResFileConst.mountEquip, this.onLoadBmResource);
            return;
        }

        public updateEquipList(): void
        {
            var loc2=null;
            var loc1=Cache.instance.pack.mountsPackCache.getMyMountEquip();
            var loc3=[];
            var loc4=0;
            while (loc4 < loc1.length) 
            {
                loc2 = new MountEquipCellInfo();
                loc2.idata = loc1[loc4];
                loc2.equipType = loc4 + 1;
                loc3.push(loc2);
                ++loc4;
            }
            this._equipList.dataProvider = new DataProvider(loc3);
            this.updateCurEquip(this._equipList.selectedItem as MountEquipCellInfo);
            return;
        }

        protected onListItemClickHandler(arg1: ListEvent): void
        {
            var loc1=arg1.item as MountEquipCellInfo;
            this.updateCurEquip(loc1);
            return;
        }

        /* internal  */updateCurEquip(arg1: MountEquipCellInfo): void
        {
            var loc1=null;
            if (arg1) 
            {
                loc1 = arg1.idata;
                if (loc1 && !(loc1 == ItemData.nullItemData) && loc1.itemExInfo) 
                {
                    if (MountConfig.instance.isMountEquipFullStat(loc1.type, loc1.color, loc1.itemExInfo.m_equip_star)) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(74215));
                        this.clean();
                        return;
                    }
                    this._equipItem.itemData = loc1;
                    this._equipNameTx.htmlText = ItemsUtil.getItemName(loc1);
                    this._eqDataUtil = new EquipmentItemDataUtil(loc1);
                    this._roundPro.setData(loc1.itemExInfo.m_equip_star, MountConfig.instance.getMountEquipStarByTypeColor(loc1.type, loc1.color));
                    this.getHeightestMount();
                    this.updateRightLeftBmTx();
                    this.updateAttSp();
                }
                else 
                {
                    this.clean();
                }
            }
            else 
            {
                this.clean();
            }
            return;
        }

        /* internal  */updateAttSp(): void
        {
            var loc1=new MountEquipAttInfo();
            loc1.starLv = this._equipItem.itemData.itemExInfo.m_equip_star;
            loc1.curData = this._equipItem.itemData;
            var loc2=new MountEquipAttInfo();
            if (this._equipItem.itemData.itemExInfo.m_equip_star + 1 > MountConfig.instance.getMountEquipStarByTypeColor(this._equipItem.itemData.type, this._equipItem.itemData.color)) 
            {
                loc2.starLv = 0;
            }
            else 
            {
                loc2.starLv = this._equipItem.itemData.itemExInfo.m_equip_star + 1;
            }
            loc2.curData = this._equipItem.itemData;
            this._leftAtt.updateInfo(loc1);
            this._rightAtt.updateInfo(loc2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.MountEquipShowUpFight, {"curArr": this._leftAtt.attArr, "nextArr": this._rightAtt.attArr}));
            return;
        }

        /* internal  */updateRightLeftBmTx(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=0;
            var loc9=0;
            if (this._equipItem.itemData && this._eqDataUtil) 
            {
                loc1 = this._eqDataUtil.getBasePropArray();
                loc2 = new Map<any, any> /* flash.utils.Dictionary */();
                loc5 = [];
                loc6 = [];
                loc7 = 0;
                while (loc7 < loc1.length) 
                {
                    loc2 = loc1[loc7] as Map<any, any> /* flash.utils.Dictionary */;
                    if ((loc4 = loc2["type"]) == EJewel._EJewelPhysicalDefense || loc4 == EJewel._EJewelMagicDefense) 
                    {
                        ++loc3;
                        loc6.push(loc2);
                    }
                    else 
                    {
                        loc5.push(loc2);
                    }
                    ++loc7;
                }
                if (loc3 != 2) 
                {
                    loc5 = [];
                    loc6 = [];
                    loc8 = loc1.length / 2;
                    loc9 = 0;
                    while (loc9 < loc1.length) 
                    {
                        loc2 = loc1[loc9] as Map<any, any> /* flash.utils.Dictionary */;
                        if (loc9 < loc8) 
                        {
                            loc5.push(loc2);
                        }
                        else 
                        {
                            loc6.push(loc2);
                        }
                        ++loc9;
                    }
                }
                this.updateCurItemAttBm(loc5, this._attBmRight);
                this.updateCurItemAttBm(loc6, this._attBmLeft);
            }
            else 
            {
                this._attBmRight.bitmapData = null;
                this._attBmLeft.bitmapData = null;
            }
            return;
        }

        /* internal  */updateCurItemAttBm(arg1: Array<any>, arg2: Object /* flash.display.Bitmap */): void
        {
            if (!this._equipItem.itemData) 
            {
                return;
            }
            var loc1=arg1[0] as Map<any, any> /* flash.utils.Dictionary */;
            if (loc1) 
            {
                arg2.bitmapData = CommonBmdUtil.getAttributeBmd(loc1["type"]);
            }
            else 
            {
                arg2.bitmapData = null;
            }
            return;
        }

        /* internal  */clean(): void
        {
            this._roundPro.setData(0, 0);
            this._equipItem.itemData = null;
            this._eqDataUtil = null;
            this._equipNameTx.htmlText = "";
            this._jpyl.itemData = null;
            this._attBmLeft.bitmapData = null;
            this._attBmRight.bitmapData = null;
            this._tcostinfo = null;
            this._rightAtt.clean();
            this._leftAtt.clean();
            return;
        }

        /* internal  */getHeightestMount(): void
        {
            var loc1=null;
            var loc2=null;
            if (this._equipItem.itemData) 
            {
                loc1 = new ItemData(this._equipItem.itemData.itemCode);
                loc2 = new ItemExInfo();
                loc2 = this._equipItem.itemData.itemExInfo.duplicate();
                loc2.m_equip_star = MountConfig.instance.getMountEquipStarByTypeColor(loc1.type, loc1.color);
                loc1.itemExInfo = loc2;
                this._jpyl.itemData = loc1;
            }
            return;
        }

        /* internal  */onLoadBmResource(): void
        {
            return;
        }

        public updateMaterial(): void
        {
            this._rightAtt.updateMaterial();
            this._leftAtt.updateMaterial();
            return;
        }

        public updateFightUp(arg1: int, arg2: Boolean): void
        {
            if (arg2) 
            {
                this._leftAtt.updateAddFightTx(arg1, arg2);
            }
            else 
            {
                this._rightAtt.updateAddFightTx(arg1, arg2);
            }
            return;
        }

        /* internal */ /* var */_eqDataUtil: EquipmentItemDataUtil;

        /* internal */ /* var */_tcostinfo: TMountEquipUpgrade;

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_jpyl: DeployPanelItem;

        /* internal */ /* var */_equipNameTx: Object /* flash.text.TextField */;

        /* internal */ /* var */_equipItem: BaseItem;

        /* internal */ /* var */_roundPro: MountEquipRoundProcess;

        /* internal */ /* var */_attBmRight: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_attBmLeft: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_leftAtt: MountEquipAttSp;

        /* internal */ /* var */_rightAtt: MountEquipAttSp;

        /* internal */ /* var */_equipList: TilePanel;
    }
